//
//  AEHomePageViewController.m
//  AESOLAR_IOS
//
//  Created by Bin Tang on 2024/10/18.
//  Copyright © 2024 Mr.Wang. All rights reserved.
//

#import "AEHomePageViewController.h"
#import "AEHomePageViewModel.h"
#import "TYZBaseTableViewController.h"
#import "TYZBaseCollectionViewController.h"
#import "AEHomePageSubViewController.h"
#import "AECommunityPublishViewController.h" // 社群发布
#import "AE_Solar-Swift.h"

@interface AEHomePageViewController ()
@property (nonatomic, strong) AEHomePageViewModel *viewModel;

/// 社群发布
@property (nonatomic, strong) UIButton *btnPublish;

/// 社群
@property (nonatomic, strong) AEHomePageSubViewController *communityVC;

@end

@implementation AEHomePageViewController

- (void)dealloc
{
    debugMethod();
}

- (void)initWithVar
{
    [super initWithVar];
    
    
}

- (void)initWithViewModel
{
    _viewModel = [AEHomePageViewModel new];
    [_viewModel setupData];
}

- (void)initWithNavBar
{
    [super initWithNavBar];
    
    self.navigationItem.title = self.viewModel.title;
    
}

- (void)initWithSubView
{
//    [self initWithTopView];
    self.dataSource = self;
    self.delegate = self;
    
    [super initWithSubView];
    
//    [self initWithBottomView];
    
    [self fromServerRequestData];
    
    [self initWithBtnPublish];
}

- (void)fromServerRequestData
{
    [self requestWithNewsGroups];
}

//

/// 社群评论按钮
- (void)initWithBtnPublish {
    if (!_btnPublish) {
        UIImage *image = [UIImage imageNamed:@"ic_main_community_publish"];
        CGRect frame = CGRectMake(kScreenW - 15 - image.size.width, self.view.height - 64 - image.size.height - NAVBAR_HEIGHT_STATUS - TABBAR_HEIGHT, image.size.width, image.size.height);
        _btnPublish = [UIButton createWithButton:self.view frame:frame font:FONTSIZE(16) target:self action:@selector(actionWithPublish:) propertiesBlock:^(UIButton * _Nonnull button) {
            [button setImage:image forState:UIControlStateNormal];
            button.hidden = YES;
        }];
    }
}

/// 社群评论按钮
/// - Parameter sender: sender description
- (void)actionWithPublish:(id)sender {
    @weakify(self);
    void(^block)(id data) = ^(id data) {
        @strongify(self);
        [self.communityVC reloadData];
    };
    AECommunityPublishViewController *publishVC = [[AECommunityPublishViewController alloc] init];
    publishVC.newsGroupId = self.communityVC.newsGroupId;
    publishVC.popResultBlock = block;
    [TYZJumpVCManager pushBaseVC:self pushVC:publishVC];
}

/// 获取新闻分组
- (void)requestWithNewsGroups {
    @weakify(self);
    [self.viewModel requestWithNewsGroups:^(EDSRespondDataEntity * _Nonnull respond) {
        @strongify(self);
        [self addSubVC];
    }];
}

- (void)addSubVC {
    NSMutableArray *titles = [NSMutableArray new];
    NSMutableArray *vcs = [NSMutableArray new];
    for (AENewsGroupsModel *model in self.viewModel.groups) {
//        if ([model.newsGroup isEqualToString:@"社群"]) {
//            [AEAppDelegateTool sharedInstance].newsGroupId = [model.id integerValue];
//        }
        if ([model.id integerValue] == 3) { // 表示社群，等上线后，再固定id是多少
            [AEAppDelegateTool sharedInstance].newsGroupId = [model.id integerValue];
        }
        [titles addObject:model.newsGroup];
        AEHomePageSubViewController *vc = [[AEHomePageSubViewController alloc] init];
        vc.newsGroup = model.newsGroup;
        vc.newsGroupId = [model.id integerValue];
        [vcs addObject:vc];
    }
    if (titles.count > 0) {
        [self insertPageChildControllersWithTitles:titles controllers:vcs index:0];
        
        [self removePageControllerWithIndex:titles.count];
    }
    
    [self setSelectedPageIndex:0];
}


+ (TYZPageConfigration *)setupConfig
{
    TYZPageConfigration *config = [TYZPageConfigration defaultConfigration];
    config.pageStyle = TYZPageStyleTop;
    config.headerViewCouldScale = NO;
    config.headerViewScaleMode = TYZPageHeaderViewScaleModeTop;
//    config.headerViewCouldScrollPage = NO;
    config.showTabbar = YES;
    config.showNavigation = YES;
    config.aligmentModeCenter = NO;
    config.lineWidthEqualFontWidth = YES; // 变为平分时 是否需要线条宽度等于字体宽度 默认 NO
    config.showBottomLine = NO;
    config.scrollMenu = NO;
//    config.itemMargin = 20;
    
    config.aligmentModeCenter = NO;
    config.lineHeight = 2;
//    config.lineWidth = 30;
    config.lineColor = kThemeColor;
    
//    config.cutOutHeight = 58;//kWDWorkOrderTopViewHeight;
//    config.suspenOffsetY = 200;//NAVBAR_HEIGHT_STATUS; // 设置悬浮停顿偏移量
//    config.tempTopHeight = 58;
    
    config.itemFont = [UIFont systemFontOfSize:16 weight:UIFontWeightBold];
    config.selectedItemFont = [UIFont systemFontOfSize:16 weight:UIFontWeightHeavy];
    config.normalItemColor = [UIColor colorWithHexString:@"#999999"];
    config.selectedItemColor = [UIColor colorWithHexString:@"#F6020A"];;
    config.scrollViewBackgroundColor = kWhiteColor;
    
    return config;
}

+ (NSArray *)setupMenuTitles {
    NSString *title = [AEStringExten localizedValueWithKey:@"社区"];
    return @[title];
}

+ (NSArray *)setupViewControllers:(NSDictionary *)param {
    NSArray *titles = [self setupMenuTitles];
    NSMutableArray *vcs = @[].mutableCopy;
    for (int i=0; i<titles.count; i++) {
        TYZBaseTableViewController *vc = [[TYZBaseTableViewController alloc] init];
        [vcs addObject:vc];
    }
    return vcs;
}


#pragma mark - TYZPageViewControllerDelegate, TYZPageViewControllerDataSource
- (UIScrollView *)pageViewController:(TYZPageViewController *)pageViewController pageForIndex:(NSInteger)index
{
//    self.viewModel.currentIndex = index;
    UIViewController *vc = pageViewController.controllerList[index];
    if ([vc isKindOfClass:[AEHomePageSubViewController class]]) {
        AEHomePageSubViewController *subVC = (AEHomePageSubViewController *)vc;
        // [subVC.newsGroup isEqualToString:@"社群"]
        if (subVC.newsGroupId == 3) { // 暂时固定位3
            self.btnPublish.hidden = NO;
        } else {
            self.btnPublish.hidden = YES;
        }
    }
    
    if ([vc isKindOfClass:[TYZBaseTableViewController class]])
    {
        UITableView *table = ((TYZBaseTableViewController *)vc).baseTableView;
//        table.contentOffset = CGPointMake(0, -100);
        return table;
    }
    else if ([vc isKindOfClass:[TYZBaseCollectionViewController class]]) {
        return ((TYZBaseCollectionViewController *)vc).baseCollectionView;;
    }
    return nil;
}

- (void)pageViewController:(TYZPageViewController *)pageViewController contentOffsetY:(CGFloat)contentOffsetY progress:(CGFloat)progress
{
    
}

- (void)pageViewController:(TYZPageViewController *)pageViewController didScrollMenuItem:(UIButton *)itemButton index:(NSInteger)index
{
    UIViewController *vc = pageViewController.controllerList[index];
    if ([vc isKindOfClass:[AEHomePageSubViewController class]]) {
        AEHomePageSubViewController *subVC = (AEHomePageSubViewController *)vc;
        // [subVC.newsGroup isEqualToString:@"社群"]
        if (subVC.newsGroupId == 3) { // 社群 暂时固定位3
            self.communityVC = subVC;
            self.btnPublish.hidden = NO;
        } else {
            self.btnPublish.hidden = YES;
        }
    }
}

@end
